package com.zhou.util;

import com.zhou.framework.constant.AppEnum;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author lang.zhou
 * @date 2019/7/19
 */
public class EncryptUtil {
    public static void main(String[] args) {
        String s = decryptDES("CE048066C965986CB1880A3DB9A240D13F62A2EDB86A4F77");
        decryptDES("mkkywhosmojldhgc");
        System.out.println(s);
        System.out.println(s.length());
    }

    public String encodeMD5(String plainText) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] cipher = md.digest(plainText.getBytes());
            StringBuilder builder = new StringBuilder();
            byte[] arr$ = cipher;
            int len$ = cipher.length;

            for(int i$ = 0; i$ < len$; ++i$) {
                byte b = arr$[i$];
                byte b1 = (byte)((b & 240) >> 4);
                byte b2 = (byte)(b & 15);
                builder.append(b1 < 10?(char)(48 + b1):(char)(97 + (b1 - 10)));
                builder.append(b2 < 10?(char)(48 + b2):(char)(97 + (b2 - 10)));
            }

            return builder.toString();
        } catch (NoSuchAlgorithmException var11) {
            return plainText;
        }
    }

    public static String encryptDES(String message) {
        return encryptDES(message, AppEnum.ENCRYPT_KEY, "UTF-8");
    }

    public static String encryptDES(String message, String key) {
        return encryptDES(message, key, "UTF-8");
    }

    public static String encryptDES(String message, String key, String encoding) {
        try {
            Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
            DESKeySpec desKeySpec = new DESKeySpec(key.getBytes(StandardCharsets.UTF_8));
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
            IvParameterSpec iv = new IvParameterSpec(key.getBytes(StandardCharsets.UTF_8));
            cipher.init(1, secretKey, iv);
            return byte2hex(cipher.doFinal(message.getBytes(encoding)));
        } catch (Exception var8) {
            return message;
        }
    }

    public static String decryptDES(String message) {
        return decryptDES(message, AppEnum.ENCRYPT_KEY);
    }

    public static String decryptDES(String message, String key) {
        try {
            byte[] bytesrc = convertHexString(message);
            Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
            DESKeySpec desKeySpec = new DESKeySpec(key.getBytes(StandardCharsets.UTF_8));
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
            IvParameterSpec iv = new IvParameterSpec(key.getBytes(StandardCharsets.UTF_8));
            cipher.init(2, secretKey, iv);
            byte[] retByte = cipher.doFinal(bytesrc);
            return new String(retByte);
        } catch (Exception var9) {
            return message;
        }
    }

    public static byte[] convertHexString(String ss) {
        byte[] digest = new byte[ss.length() / 2];

        for(int i = 0; i < digest.length; ++i) {
            String byteString = ss.substring(2 * i, 2 * i + 2);
            int byteValue = Integer.parseInt(byteString, 16);
            digest[i] = (byte)byteValue;
        }

        return digest;
    }

    public static String byte2hex(byte[] b) {
        StringBuilder hs = new StringBuilder();
        String stmp = "";

        for (byte value : b) {
            stmp = Integer.toHexString(value & 255);
            if (stmp.length() == 1) {
                hs.append("0").append(stmp);
            } else {
                hs.append(stmp);
            }
        }

        return hs.toString().toUpperCase();
    }
}
